package com.boot.app.development.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.api.ApiResult;
import com.boot.app.development.dto.DemoDto;
import com.boot.app.development.service.IDevelopmentDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("demo")
public class DevelopmentDemoController {

    @Autowired
    private IDevelopmentDemoService demoService;

    @GetMapping("{id}")
    public ApiResult<DemoDto> getDemo(@PathVariable("id") long id){
        DemoDto demoDto = demoService.getDemoDtoById(id);
        return ApiResult.success(demoDto);
    }

    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<DemoDto>> getDemoPage(@PathVariable int page, @PathVariable int size, DemoDto demoDto){
        Page<DemoDto> demoDtoPage = demoService.pageDemoDto(new Page<>(page, size), demoDto);
        return ApiResult.success(demoDtoPage);
    }

    @PostMapping()
    public ApiResult<?> postOrPutDemo(@RequestBody DemoDto demoDto){
        demoService.saveOrUpdateDemoDto(demoDto);
        return ApiResult.success();
    }

    @DeleteMapping("{id}")
    public ApiResult<?> deleteDemo(@PathVariable("id") long id){
        demoService.removeDemoDtoById(id);
        return ApiResult.success();
    }

}
